/**
 * 
 */
package gamePlay;

import gui.GUI_manager;
import gui.LoaderGif;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import Utils.Concurrent.AsyncTask;
import Utils.Concurrent.LoaderRunner;
import Utils.Concurrent.ThreadTasks.NextChainTask;
import main.Session;
import main.SharedPref;
import datatype.Question;

/**
 * @author Yuval 
 * extends the AbsGame, and add some fields and functions for the
 *         Level type, and also create the thread for fetching the questions if
 *         Needed
 */
public class LevelMod extends AbsGame {

	private ChainOfLevel nextLevel = null;
	private int numberOfLevels = SharedPref.levelModLengths.length;
	private ChainOfLevel CurrentLevel;
	private NextChainTask nextLevelTask;
	// private LoaderRunner loaderR;
	private LoaderGif lgForStart = new LoaderGif(
			Session.gui.getStart_game_loadingCNVS(), 1);
	private LoaderGif lgForAll = new LoaderGif(
			Session.gui.getNextLevel_loadingCNVS(), 1);

	public synchronized ChainOfLevel getNextLevel() {
		return nextLevel;
	}

	public synchronized void setNextLevel(ChainOfLevel nextLevel) {
		this.nextLevel = nextLevel;
	}

	public synchronized ChainOfLevel getCurrentLevel() {
		return CurrentLevel;
	}

	public synchronized void setCurrentLevel(ChainOfLevel currentLevel) {
		CurrentLevel = currentLevel;
	}

	public synchronized NextChainTask getNextLevelTask() {
		return nextLevelTask;
	}

	public synchronized void setNextLevelTask(NextChainTask nextLevelTask) {
		this.nextLevelTask = nextLevelTask;
	}

	public synchronized int getNumberOfLevels() {
		return numberOfLevels;
	}

	public LevelMod(int numberOfLevels) {
		super();
		this.numberOfLevels = numberOfLevels;
		AsyncGetLevel();

	}

	@Override
	public Question nextQuestion() {
		if (CurrentLevel.getLevel() != numberOfLevels) {
			if (!isAskForMore && CurrentLevel.getRatio() >= 0.5) {
				isAskForMore = true;
				AsyncGetLevel();
			}
		}

		return CurrentLevel.nextQuestion();
	}

	private void AsyncGetLevel() {

		GUI_manager gui = Session.gui;

		String loadingSTR = (chain == null) ? "Loading level, please wait"
				: "Loading next level, please wait";
		/*
		 * Canvas canvas = (chain == null) ? gui.getStart_game_loadingCNVS() :
		 * gui .getNextLevel_loadingCNVS();
		 */
		LoaderGif lg = (chain == null) ? lgForStart : lgForAll;
		Button button = (chain == null) ? gui.getStart_game_startBTN() : gui
				.getNextLevel_nextBTN();
		// loaderR = new LoaderRunner(gui.getDisplay(), canvas,
		// loadingSTR);

		button.setVisible(false);
		nextLevelTask = new NextChainTask(Session.gui.getDisplay(), button, lg);

		Session.poolTheQuestions.pushUrgentTask(lg.getLoaderTask(),
				nextLevelTask);

		// Session_new.poolTheQuestions.addTaskToPool(true, nextLevelTask);
		// Session_new.poolTheQuestions.addTaskToPool(true, loaderR);

	}

	public boolean haveMoreLevels() {
		return CurrentLevel.getLevel() < numberOfLevels;
	}

	public void nextLevel() {
		//System.out.println(CurrentLevel.getLevel()+" next is: "+nextLevel.getLevel() );
		chain = nextLevel;
		CurrentLevel = nextLevel;
		isAskForMore = false;

	}

}
